package cvo.chess.client.entities;

import java.util.ArrayList;
import java.util.List;

import cvo.chess.client.entities.chessboard.ChessboardCoordinate;
import cvo.chess.client.entities.chessboard.IChessboardCoordinate;
import cvo.chess.client.entities.chessboard.ITile;
import cvo.chess.client.entities.game.IChessboard;
import cvo.chess.client.entities.game.IPiece;



	
public class GameFacade implements IGameFacade {

	private static List<GameFacade> instances = new ArrayList<GameFacade>();
	private Integer id; 

	public static GameFacade get(Integer id) {
	
		return new GameFacade();
	}
	
	


	private Game game;
	public Game newGame() {




		Game g = new Game(this);
		this.game = g;
 
		return g;
	}

		
	public IChessboard getBoard() {



		return this.game.getBoard();
	}
	
	public IPiece findPieceAt(IChessboardCoordinate boardCoordinate){
		return this.game.findPieceAt(boardCoordinate);
	}

	public void startGame() throws Exception {
		this.game.init();
		this.game.start();
	}

		
	public IPiece getPieceAt(IChessboardCoordinate boardCoordinate) {



		return this.game.findPieceAt(boardCoordinate);
	}

	

	public void setPieceAtPosition(IPiece piece, IChessboardCoordinate position)
			throws Exception {


		this.game.setPiece(piece, position);
	}
	
	
	@Override
	public void setPieceAtPosition(IPiece piece, String A1notation)
			throws Exception {
		ChessboardCoordinate cc = new ChessboardCoordinate( this.game.getBoard(), A1notation);
		this.game.setPiece(piece, cc);
		
	}	
	
	
	
	

	public ITile getTileAt(String a1notation) {
		return this.getBoard().getTileAt(new ChessboardCoordinate(this.getBoard(), a1notation));
	}

	public IPiece getPieceAt(String a1notation) {
		return this.game.findPieceAt(new ChessboardCoordinate(this.getBoard(), a1notation));
	}


	public IChessboardCoordinate getPositionOfPiece(IPiece piece) throws Exception{
		return this.game.getPositionOf(piece);
		
	}



	public void doMove(String from, String to) throws Exception {
		//TODO: never trust your gui... so, validate input again
		//Throw exception when not valid. You may 'punish' the gui for bad input validation 
		// TODO: never trust your gui... so, validate input again
		// Throw exception when not valid. You may 'punish' the gui for bad
		// input validation
		IChessboardCoordinate fromBc = new ChessboardCoordinate(getBoard(), from);
		ChessboardCoordinate toBc = new ChessboardCoordinate(getBoard(), to);
		

		this.game.doMove(fromBc, toBc);
		//TODO: get chessboardWidget and call method "clearPossibleMovePosition()" 
		
	}




}
